/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.daos.EmployeeDao;
import com.insurance.cis.daos.GroupDao;
import com.insurance.cis.models.Employee;
import com.insurance.cis.models.Gender;
import com.insurance.cis.models.Group;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;

/**
 *
 * @author TruongLQ
 */
public class EmployeeJDialogConfirmationView extends javax.swing.JDialog {

    Employee employee;
    JDialog dialog;
    EmployeeDao employeeDao = new EmployeeDao();
    GroupDao groupDao = new GroupDao();

    /**
     * Creates new form EmployeeJDialogCreationView
     */
    public EmployeeJDialogConfirmationView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private void loadGroups() {
        Vector<Group> groups = groupDao.getAllGroups();
        groups.add(0, new Group(null, "Select a group"));
        cbxGroups.setModel(new DefaultComboBoxModel(groups));
    }
    public EmployeeJDialogConfirmationView(JDialog dialog, boolean modal, Employee employee) {
        super(dialog, modal);
        initComponents();
        this.employee = employee;
        this.dialog = dialog;
        loadGroups();
        loadConfirmationInfo();
        this.setResizable(false);
        this.setLocationRelativeTo(getRootPane());
    }

    private void loadConfirmationInfo() {
        if (employee != null) {
            txtEmpID.setText(employee.getCode());
            txtUserName.setText(employee.getUserName());
            txtPwd.setText(employee.getPassword());
            txtConfimPwd.setText(employee.getPassword());
            txtFullName.setText(employee.getName());
            if (employee.getDob()!=null) {
                txtBirthday.setText(Utils.convertDateToString(employee.getDob(), CisConstants.DATE_DD_MM_YYYY));
            }
            cbxGroups.setSelectedItem(employee.getGroup());
            setGender();
        }
    }

    private void setGender() {
        if (employee != null) {
            if (Gender.MALE.getDesc().equals(employee.getGender())) {
                rbtnMale.setSelected(true);
            } else if (Gender.FEMALE.getDesc().equals(employee.getGender())) {
                rbtnFemale.setSelected(true);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtEmpID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPwd = new javax.swing.JPasswordField();
        txtConfimPwd = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        rbtnMale = new javax.swing.JRadioButton();
        rbtnFemale = new javax.swing.JRadioButton();
        txtBirthday = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbxGroups = new javax.swing.JComboBox();
        txtUserName = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnYes = new javax.swing.JButton();
        btnNo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Confirmation Dialog");

        txtEmpID.setEditable(false);

        jLabel1.setText("Employee ID");

        jLabel4.setText("Group");

        jLabel2.setText("User Name");

        jLabel5.setText("Confirm Password");

        txtPwd.setEditable(false);

        txtConfimPwd.setEditable(false);

        jLabel6.setText("Full Name");

        jLabel7.setText("Gender");

        jLabel8.setText("Birthday");

        txtFullName.setEditable(false);

        rbtnMale.setText("Male");
        rbtnMale.setEnabled(false);

        rbtnFemale.setText("Female");
        rbtnFemale.setEnabled(false);

        txtBirthday.setEditable(false);

        jLabel3.setText("Password");

        cbxGroups.setEnabled(false);

        txtUserName.setEditable(false);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Do you want to save changes?");

        btnYes.setText("Yes");
        btnYes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYesActionPerformed(evt);
            }
        });

        btnNo.setText("No");
        btnNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnYes)
                            .addGap(18, 18, 18)
                            .addComponent(btnNo, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel5)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2))
                            .addGap(32, 32, 32)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtConfimPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(39, 39, 39)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel4))
                            .addGap(37, 37, 37)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtBirthday)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(rbtnMale)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(rbtnFemale))
                                .addComponent(txtFullName)
                                .addComponent(cbxGroups, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel5});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel4, jLabel6, jLabel7, jLabel8});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnNo, btnYes});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(rbtnMale)
                    .addComponent(rbtnFemale))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtConfimPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cbxGroups, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnYes)
                    .addComponent(btnNo))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNoActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnNoActionPerformed

    private void btnYesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnYesActionPerformed
        if (dialog instanceof EmployeeJDialogCreationView) {
            createEmployee();
        } else if (dialog instanceof EmployeeJDialogUpdateView) {
            updateEmployee();
        }
    }//GEN-LAST:event_btnYesActionPerformed

    private void createEmployee() {
        if (!employeeDao.createEmployee(employee)) {
            MessageDialogUtils.dialogError(this, "Error while creating new employee");
        } else {
            MessageDialogUtils.dialogMessage(this, "Create new employee succesful");
        }
        this.dispose();
        ((EmployeeJDialogCreationView)dialog).mainJFrame.getEmpManagementJpnl().refreshSearchResult();
        dialog.dispose();
    }

    private void updateEmployee() {
        if (!employeeDao.updateEmployee(employee)) {
            MessageDialogUtils.dialogError(this, "Error while updating a employee");
        } else {
            MessageDialogUtils.dialogMessage(this, "Update employee succesful");
        }
        this.dispose();
        ((EmployeeJDialogUpdateView)dialog).mainJFrame.getEmpManagementJpnl().refreshSearchResult();
        dialog.dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNo;
    private javax.swing.JButton btnYes;
    private javax.swing.JComboBox cbxGroups;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton rbtnFemale;
    private javax.swing.JRadioButton rbtnMale;
    private javax.swing.JTextField txtBirthday;
    private javax.swing.JPasswordField txtConfimPwd;
    private javax.swing.JTextField txtEmpID;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JPasswordField txtPwd;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
